package name.elftesting.vncelf;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.ImageIcon;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.UIManager;

import name.elftesting.vnctest.VncElf;

/**
 * 
 * @author Xiaoping Wu
 * 
 */
public class LoginFrame extends JFrame {
	private static final long serialVersionUID = -3374450823823567437L;

	private static final JLabel hostLabel = new JLabel("Host:");
	private static final JComboBox hostInput = new JComboBox();

	private static final JLabel portLabel = new JLabel("Port:");
	private static final JTextField portInput = new JTextField("5900");
	
	private static final JLabel passwordLabel = new JLabel("Password:");
	private static final JPasswordField passwordInput = new JPasswordField("");
	
	private static final JLabel messageLabel = new JLabel("");
	JButton loginButton = new JButton("Connect");
	JButton exitButton = new JButton("Cancel");

	public LoginFrame(String message) {
		
		setSize(new Dimension(380, 210));
		setTitle("Vncelf");
		setLayout(null);

		initUI();
		messageLabel.setText(message);
		messageLabel.setForeground(Color.red);
		ImageIcon image = new ImageIcon(this.getClass().getResource(
				"/images/vnc.png"));

		this.setIconImage(image.getImage());
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		Dimension screen = getToolkit().getScreenSize();
		// pack();
		setLocation((screen.width - getSize().width) / 2,
				(screen.height - getSize().height) / 2);

		loginButton.registerKeyboardAction(loginButton.getActionForKeyStroke(KeyStroke
		.getKeyStroke(KeyEvent.VK_SPACE, 0, false)),
		KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false),
		JComponent.WHEN_IN_FOCUSED_WINDOW);

		loginButton.registerKeyboardAction(loginButton.getActionForKeyStroke(KeyStroke
		.getKeyStroke(KeyEvent.VK_SPACE, 0, true)),
		KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true),
		JComponent.WHEN_IN_FOCUSED_WINDOW);
		
		
		loginButton.addActionListener(new ActionListener() {
			public void writeLine(List lines) {
				try {
					File file = new File(".\\bin\\host.clf");

					PrintWriter out = new PrintWriter(
							new FileOutputStream(file), true);
					for (int i = 0; i < lines.size(); i++)
						out.println(lines.get(i));

				} catch (Exception e) {

					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			public void actionPerformed(ActionEvent e) {
				String userName = hostInput.getSelectedItem().toString()
						.trim();

		
				if (userName.equals("")) {

					messageLabel.setText("* Please input host ip address");
					messageLabel.setForeground(Color.red);
					return;
				}
				List l = new ArrayList();
				l.add(hostInput.getEditor().getItem());

				for (int i = 0; i < hostInput.getItemCount(); i++) {
					Object item = hostInput.getItemAt(i);
					if (!l.contains(item))
						l.add(item);
				}
				this.writeLine(l);
				dispose();
				
				String[] text=null;
				
				if(passwordInput.getPassword().length>0)
					text = new String[] { "host", userName, "port",
						portInput.getText().trim(),"password",new String(passwordInput.getPassword()) };
				else
					text = new String[] { "host", userName, "port",
						portInput.getText().trim() };
				VncElf vr = new VncElf();
				VncViewer v = new VncViewer();
				v.mainArgs = text;
				v.inAnApplet = false;
				v.inSeparateFrame = true;

				v.init();
				v.start();
				vr.init(v);
				
			}
		});

		exitButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
	}



	public List readLine() {
		File file = new File(".\\bin\\host.clf");
		List l = new ArrayList();
		if (!file.exists())
			try {
				file.createNewFile();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		FileInputStream os;

		try {

			BufferedReader br = new BufferedReader(new FileReader(file));

			String line = "";
			while ((line = br.readLine()) != null) {
				l.add(line);
			}
		} catch (Exception e) {

			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return l;
	}

	private void initUI() {
	
		// TODO Auto-generated method stub
		hostLabel.setBounds(10, 10, 70, 21);
		hostInput.setBounds(90, 10, 200, 21);
		portLabel.setBounds(10, 40, 70, 21);
		portInput.setBounds(90, 40, 200, 21);
		passwordLabel.setBounds(10, 70, 70, 21);
		passwordInput.setBounds(90, 70, 200, 21);
		messageLabel.setBounds(10,90,270,21);
		loginButton.setBounds(90, 130, 100, 21);
		exitButton.setBounds(200, 130, 100, 21);
		hostInput.setEditable(true);
		List str = this.readLine();
		for (int i = 0; i < str.size(); i++) {
			String s = str.get(i).toString();
			hostInput.addItem(s);
		}
		if (hostInput.getItemCount() > 0)
			hostInput.setSelectedIndex(0);
		add(hostLabel);
		add(hostInput);
		add(portLabel);
		add(portInput);
		add(passwordLabel);
		add(passwordInput);
		add(messageLabel);
		add(loginButton);
		add(exitButton);
	}

//	public void setMessageLabel(String message){
//		LoginFrame.messageLabel.setText(message);
//	}
	public static void main(String[] args) {
	
		new LoginFrame("");
	}
}
